<template>
  <a-spin :spinning="confirmLoading">
    <j-form-container :disabled="formDisabled">
      <a-form-model ref="form" :model="model" :rules="validatorRules" slot="detail">
        <a-row>
          <a-col :span="8">
            <a-form-model-item label="票据单号" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="billNum">
              <a-input v-model="model.billNum" placeholder="" readOnly :disabled="disabled" ></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="8">
            <a-form-model-item label="姓名" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="name">
              <a-input v-model="model.name" placeholder="" readOnly :disabled="disabled" ></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="8">
            <a-form-model-item label="性别" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="sex">
              <j-dict-select-tag type="list" v-model="model.sex" dictCode="sex" placeholder="" disabled/>
            </a-form-model-item>
          </a-col>
          <a-col :span="8">
            <a-form-model-item label="身份证号" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="idNum">
              <a-input v-model="model.idNum" placeholder="" readOnly :disabled="disabled" ></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="8">
            <a-form-model-item label="余额" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="balance">
              <a-input-number v-model="model.balance" placeholder="" style="width: 100%" disabled/>
            </a-form-model-item>
          </a-col>
          <a-col :span="8">
            <a-form-model-item label="监室号" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="orgCode">
              <j-select-depart v-model="model.orgCode" multi disabled />
            </a-form-model-item>
          </a-col>
          <a-col :span="8">
            <a-form-model-item label="人员状态" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="status">
              <j-dict-select-tag type="list" v-model="model.status" dictCode="fpms_suspects_status" placeholder="请选择人员状态" disabled/>
            </a-form-model-item>
          </a-col>
          <a-col :span="8">
            <a-form-model-item label="入监日期" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="custodyDate">
              <j-date placeholder="" v-model="model.custodyDate"  style="width: 100%" disabled/>
            </a-form-model-item>
          </a-col>
          <a-col :span="8">
            <a-form-model-item label="总充值金额" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="rechargeAmount">
              <a-input-number v-model="model.rechargeAmount" placeholder="" style="width: 100%" readOnly :disabled="disabled"/>
            </a-form-model-item>
          </a-col>
          <a-col :span="8">
            <a-form-model-item label="总消费金额" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="consumptionAmount">
              <a-input-number v-model="model.consumptionAmount" placeholder="" style="width: 100%" readOnly :disabled="disabled"/>
            </a-form-model-item>
          </a-col>
          <a-col :span="8">
            <a-form-model-item label="出监日期" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="releaseDate">
              <j-date placeholder="" v-model="model.releaseDate"  style="width: 100%" disabled/>
            </a-form-model-item>
          </a-col>
          <a-col :span="8">
            <a-form-model-item label="退款金额" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="refundAmount">
              <a-input-number :min="0" v-model="model.refundAmount" placeholder="请输入退款金额" style="width: 100%" />
            </a-form-model-item>
          </a-col>
          <a-col :span="8">
            <a-form-model-item label="退款日期" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="refundDate">
              <j-date placeholder=""  v-model="model.refundDate" :show-time="true" date-format="YYYY-MM-DD HH:mm:ss" style="width: 100%" disabled/>
            </a-form-model-item>
          </a-col>
          <!--
          <a-col :span="8">
            <a-form-model-item label="证件信息" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="certificateInfo">
              <a-textarea v-model="model.certificateInfo" rows="4" placeholder="请输入证件信息" />
            </a-form-model-item>
          </a-col>
          -->
          <a-col :span="8">
            <a-form-model-item label="经办人" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="transactor">
              <a-input v-model="model.transactor" placeholder="" readOnly :disabled="disabled" ></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="8">
            <a-form-model-item label="备注" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="remark">
              <a-input v-model="model.remark" placeholder="请输入备注"  ></a-input>
            </a-form-model-item>
          </a-col>
        </a-row>
      </a-form-model>
    </j-form-container>
  </a-spin>
</template>

<script>

  import { httpAction, putAction } from '@/api/manage'
  import { validateDuplicateValue } from '@/utils/util'
  import moment from 'dayjs'

  const ruleBaseURL = '/sys/fillRule/executeRuleByCode/'

  export default {
    name: 'FpmsTksjForm',
    components: {
    },
    props: {
      //表单禁用
      disabled: {
        type: Boolean,
        default: false,
        required: false
      }
    },
    data () {
      return {
        model:{
        },
        labelCol: {
          xs: { span: 24 },
          sm: { span: 5 },
        },
        wrapperCol: {
          xs: { span: 24 },
          sm: { span: 16 },
        },
        confirmLoading: false,
        validatorRules: {
           billNum: [
              { required: true, message: '请输入票据单号!'},
           ],
           idNum: [
              { required: true, message: '请输入身份证号!'},
           ],
           balance: [
              { required: true, message: '请输入余额!'},
           ],
           refundAmount: [
              { required: true, message: '请输入退款金额!'},
           ],
        },
        url: {
          add: "/finance/fpmsTksj/add",
          edit: "/finance/fpmsTksj/edit",
          queryById: "/finance/fpmsTksj/queryById",
          rule: {
            billNum: ruleBaseURL + 'shop_order_num'
          },
        }
      }
    },
    computed: {
      formDisabled(){
        return this.disabled
      },
    },
    created () {
       //备份model原始值
      this.modelDefault = JSON.parse(JSON.stringify(this.model));
    },
    methods: {
      add () {
        let record = this.modelDefault
        if(!record.billNum){
          this.getBillNum(record);     
        }
        if(!record.transactor){
          record.transactor = this.$store.state.user.realname;
        }
        if(!record.refundDate){
          record.refundDate = moment(new Date()).format('YYYY-MM-DD HH:mm:ss');
        }
        this.edit(record);
      },
      edit (record) {
        if(!record.billNum){
          this.getBillNum(record);     
        }
        this.model = Object.assign({}, record);     
        this.visible = true; 
      },
      getBillNum(record) {
        putAction(this.url.rule.billNum, this.model).then(res => {
          // 执行成功，获取返回的值，并赋到页面上
          if (res.success) {
            record.billNum = res.result;
            this.model = Object.assign({}, record);     
            this.visible = true;  
          }
        })
      },
      submitForm () {
        const that = this;
        // 触发表单验证
        this.$refs.form.validate(valid => {
          if (valid) {
            that.confirmLoading = true;
            let httpurl = '';
            let method = '';
            if(!this.model.id){
              httpurl+=this.url.add;
              method = 'post';
            }else{
              httpurl+=this.url.edit;
               method = 'put';
            }
            httpAction(httpurl,this.model,method).then((res)=>{
              if(res.success){
                that.$message.success(res.message);
                that.$emit('ok');
                that.$router.push({ path: '/finance/FpmsTksj' });
              }else{
                that.$message.warning(res.message);
              }
            }).finally(() => {
              that.confirmLoading = false;
            })
          }
         
        })
      },
    }
  }
</script>